<template>
  <section v-if="props.show" class="popup-container flex flex-js-c flex-ai-c" @click="close" @mousewheel="onMouse">
    <div
      :class="['body position-relative', props.show ? 'animation' : '']"
      @click.stop="() => {}">
      <slot name="header"></slot>
      <div class="content" :style="contentStyle">
        <slot></slot>
      </div>
      <template v-if="footerDefault">
        <div class="footer flex flex-js-c flex-ai-c">
          <button
              @click="handleConfirm('Confirm')"
              class="button bg-color-main margin-right-sm">
              确认
          </button>
          <button
              @click="handleConfirm('Cancel')"
              class="button bg-color-gray-f3">
              关闭
          </button>
        </div>
      </template>
      <template>
        <slot name="footer"></slot>
      </template>
    </div>
  </section>
</template>

<script lang="ts" setup>
  import { computed } from "vue";

  let props = defineProps({
    // 是否禁止背景滚动
    disableScroll: {
      type: Boolean,
      default: false
    },
    // 使用组件底部默认样式
    footerDefault: {
      type: Boolean,
      default: true
    },
    show: {
      type: Boolean,
      default: false,
    },
    height: {
      type: Number,
      default: 500
    },
    width: {
      type: Number,
      default: 500
    }
  });

  const emit = defineEmits(["update:show", 'onCancel', 'onConfirm']);

  /**
   * 样式绑定
   */
  const contentStyle = computed(() => ({
    height: props.height + 'px',
    width: props.width + 'px',
    backgroundColor: 'white'
  }));

  const close = () => emit("update:show", false);

  const handleConfirm = (e: 'Confirm' | 'Cancel') => {
    close();
    emit("on" + e as any);
  }

  /**
   * 阻止页面滚动
   * @param e
   */
  const onMouse = (e: Event) => props.disableScroll && e.preventDefault();
</script>

<style lang="scss" scoped>
  @keyframes scale {
    from {
      transform: scale(0);
    }
    to {
      transform: scale(1);
    }
  }
  .popup-container {
    top: 0;
    position: fixed;
    height: 100%;
    width: 100%;
    background-color: rgb(0, 0, 0, 0.4);
    z-index: 9999;
    .body {
      border-radius: 4Px;
      overflow: hidden;
      &.animation {
        animation: scale .5s;
      }
    }
    .content {
      overflow-y: scroll;
    }
    .footer {
      height: 55Px;
      background-color: #FFFFFF;
      .button {
        height: 28Px;
        width: 100Px;
        border: none;
        cursor: pointer;
        border-radius: 5Px;
        &:first-child {
          color: #FFFFFF;
          border: 1Px solid $color-main;
        }
        &:last-child {
          background-color: white;
          border: 1Px solid $color-gray-e5;
        }
      }
    }
  }
</style>
